package edu.ujf.sharepas.rcp;


import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.internal.win32.KEYBDINPUT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.registry.KeywordRegistry;
import org.eclipse.ui.keys.Key;
import org.eclipse.ui.part.ViewPart;

import edu.ujf.sharepas.impl.Chat;
import edu.ujf.sharepas.impl.Message;
import edu.ujf.sharepas.impl.User;
import edu.ujf.sharepas.rmi.*;



public class ChatView extends ViewPart implements Observer,Serializable{

	public static final String ID = "edu.ujf.sharepas.rcp.chatView";  //  @jve:decl-index=0:
	private Text text1 = null;
	private Text text = null;
	private Serveur serveur ;  //  @jve:decl-index=0:
	private Client client ;  //  @jve:decl-index=0:
	private Chat chat;
	
	public void createPartControl(Composite parent) {

		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = false;
		gridData.grabExcessVerticalSpace = false;
		gridData.heightHint = -1;
		gridData.widthHint = -1;
		gridData.verticalAlignment = GridData.CENTER;
		Composite top = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.numColumns = 2;
		layout.marginWidth = 0;
		top.setLayout(layout);
		// top banner
		text = new Text(top, SWT.MULTI | SWT.WRAP);
		text.setBackground(new Color(null, 250, 250, 250));
		
		// setup bold font
		text.setEditable(false);
		text.setLayoutData(new GridData(GridData.FILL_BOTH));
		Label filler = new Label(top, SWT.NONE);

		text1 = new Text(top, SWT.BORDER);
		text1.setEditable(true);
		text1.setLayoutData(gridData);
		text1.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e){
				// nothing
			}
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.keyCode == 13){
					String s = null;
					s = text1.getText() ;
					text1.setText("");
					try {
						Message m = new Message(s,client.getUser().getName());
						for(User u:client.getUser().getFriends()){
							Client client = u.getClient();
							if( client != null )
								client.send(m);
						}
						client.send(m);
						//serveur.message(new Message(s,client.getUser().getName()));
					//	client.send(new Message(s));
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}
				}	
			}
		});

	
	}

	
	public void addText(final String message){
		System.out.println("addText : "+message);

		Display.getDefault().asyncExec(new Runnable(){

			@Override
			public void run() {
				text.append(message);
				//text.setText(message);
			}		
		});
	}

	public void setServeur(Serveur s) {
		this.serveur = s;
		
	}
	
	@Override
	public void dispose(){
		super.dispose();
	}
	
	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);

		try {
			client = Application.c ;
			this.setServeur(client.getServeur());
			// creation de l'objet Chat
			chat = new Chat(client);
			chat.addObserver(this);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		this.addText(((Chat)arg0).getMessage().getUsername()+" : "+((Chat)arg0).getMessage().getMessage()+"\n");
	}


	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

} 
